
***** Measure deterrence effects with synthetic controls

local StJoTreat = mofd(mdy(11,16,2004))
local StJoSettle = mofd(mdy(12,21,2007))

local WheatonTreat = mofd(mdy(10,19,2004))
local WheatonSettle = mofd(mdy(1,7,2010))

local ElCentroTreat = mofd(mdy(3,20,2006))
local ElCentroSettle = mofd(mdy(9,20,2010))

local OverlookTreat = mofd(mdy(4,25,2008))
local OverlookSettle = mofd(mdy(6,21,2012))

local MortonPlantTreat = mofd(mdy(8,27,2008)) 
local MortonPlantSettle = mofd(mdy(11,20,2012))

local ShandsTreat = mofd(mdy(8,30,2008))
local ShandsSettle = mofd(mdy(8,8,2013))

local chsTreat = mofd(mdy(1,7,2009))
local chsSettle= mofd(mdy(8,4,2014))

cd /homes/nber/jetson-dua52260/poterba-DUA52260/jetson-dua52260/ipop/robustness/forwardonly

local dftlist "StJo Wheaton ElCentro Overlook MortonPlant Shands chs"

foreach dft of local dftlist {

	cd /homes/nber/jetson-dua52260/poterba-DUA52260/jetson-dua52260/ipop/robustness/forwardonly

	** Keep only defendant 
	use ipop_controls_panel.dta, clear
	keep if prvdrnum == "`dft'"

	merge 1:1 dt using "synth_fit_`dft'.dta"

	local treatment_period = ``dft'Treat'


	** Produce difference in series
	gen deterrence = fit1 - out if dt >= `treatment_period'

	** Time discount the difference at 10% 
	local r = 1.1^(1/12)

	gen deterrence_disc = deterrence*1/(`r'^(dt-`treatment_period'))
	replace deterrence_disc = . if dt-`treatment_period' >= 60
	count if !mi(deterrence_disc)
	assert(r(N)==60)

	*** Compute total
	preserve
	collapse (sum) deterrence_disc
	qui summarize deterrence_disc
	local deterrence_`dft' = r(min)
	restore

	*** Truncate graph at 5 years 
	drop if dt  - `treatment_period' >= 60
	summarize dt 
	local minperiod = r(min)
	local maxperiod = r(max)

	** Find max Y 
	summarize fit1
	local ymax = 1.1*r(max)

	format dt %tmMon_YY
	twoway (scatter pmt_amt dt, mcolor(gs5)) (line out fit1 dt, lwidth(thick thick) lcolor(navy maroon) lpattern(solid dash)), xline(`treatment_period', lcolor(maroon)) xline(``dft'Settle', lcolor(black)) legend(order(2 "`dft'" 3 "Synthetic Control") width(100)) xscale(range(`minperiod' `maxperiod')) xlabel(468 492 516 ``dft'Treat' ``dft'Settle' 600) xtitle("Month") graphregion(color(white)) ylabel(#2, format(%13.0fc)) yscale(range(0 `ymax'))
	* just for CHS for edited overlay
	* twoway (scatter pmt_amt dt, mcolor(gs5)) (line out fit1 dt, lwidth(thick thick) lcolor(navy maroon)), xline(`treatment_period', lcolor(maroon)) xline(``dft'Settle', lcolor(black)) legend(off) xscale(range(`minperiod' `maxperiod')) xlabel(468 492 516 ``dft'Treat' ``dft'Settle' 600) xtitle("Month") ytitle("$/Month") graphregion(color(white)) ylabel(#3, format(%13.0fc)) yscale(range(100000000 `ymax')) title("Unnecsesary Inpatient Admissions (CHS)")

	graph save "graphs/`dft'Overlay.gph", replace
}


cd /homes/nber/jetson-dua52260/poterba-DUA52260/jetson-dua52260/ipop/robustness/forwardonly

log using deterrence.log, replace
local dftlist "StJo Wheaton ElCentro Overlook MortonPlant Shands chs"
local total = 0 
foreach dft of local dftlist {
	disp "`dft' Deterrence:"
	disp `deterrence_`dft''
	local total = `total' + `deterrence_`dft''
}
disp `total'
log close 



* ** Manually edit graphs
cd /homes/nber/jetson-dua52260/poterba-DUA52260/jetson-dua52260/ipop/robustness/forwardonly/graphs


graph use "StJoOverlay.gph"
graph export "StJoOverlay.pdf", replace

graph use "WheatonOverlay.gph"
graph export "WheatonOverlay.pdf", replace


graph use "ElCentroOverlay.gph"
graph export "ElCentroOverlay.pdf", replace


graph use "OverlookOverlay.gph"
graph export "OverlookOverlay.pdf", replace


graph use "MortonPlantOverlay.gph"
graph export "MortonPlantOverlay.pdf", replace


graph use "ShandsOverlay.gph"
graph export "ShandsOverlay.pdf", replace


*** Manually clean for multipanel
graph use "chsOverlay.gph"
	** Add title
	** Y axis to $Mil labeling
	** Legend name
	** Clean up X axis dates 
graph save "chsOverlayForward.gph", replace
graph export "chsOverlayForward.pdf", replace


**** Multipanel
cd /homes/nber/jetson-dua52260/poterba-DUA52260/jetson-dua52260/ipop/robustness/forwardonly/graphs
graph combine StJoOverlay.gph WheatonOverlay.gph ElCentroOverlay.gph, rows(1) xsize(10) ysize(2.5) l1title("$/Month") saving("OtherDftTop.gph", replace)
graph combine OverlookOverlay.gph MortonPlantOverlay.gph ShandsOverlay.gph, rows(1) xsize(10) ysize(2.5) l1title("$/Month") saving("OtherDftBottom.gph", replace)
graph combine OtherDftTop.gph OtherDftBottom.gph, rows(2) xsize(10) ysize(5)
	
	** Make sure names are visible
	** Make sure y-axis and x-axis isn't cramped

graph save "MultiPanel_otherdft.gph", replace
graph export "MultiPanel_otherdft.pdf", replace

